package jcer.core;

import java.nio.channels.SelectionKey;

public class ConnectionWriteHandler extends BaseEventHandler {

	ConnectionWriteHandler(EventDriver driver) {
		super(driver);
	}

	@Override
	public void execute(Event event) throws Exception {
		Connection conn = event.conn;

		conn.write();

		if (conn.writeBuf.remaining() == 0) {
			// has writed all bytes
			// update state
			conn.state = Connection.State.READ_HEADER;
			conn.readBuf.position(0);
			conn.readBuf.limit(Packet.getHeaderLen());
			conn.socket.register(conn.selector, SelectionKey.OP_READ, conn);
		} else {
			// go on to wait write
			conn.socket.register(conn.selector, SelectionKey.OP_WRITE, conn);
		}

	}

	@Override
	public void init() {
	}

}
